<template>
  <el-form
    ref="form"
    :label-position="device === 'mobile' ? 'top' : 'right'"
    label-width="100px"
    :model="form"
    :rules="rules"
  >
    <el-form-item label="批次" prop="batchCode">
      <el-input
        v-model.trim="form.batchCode"
        autocomplete="off"
        placeholder="请输入批次"
      />
    </el-form-item>
    <el-form-item label="圈舍" prop="penHouse">
      <el-input
        v-model.trim="form.penHouse"
        autocomplete="off"
        placeholder="请输入圈舍"
      />
    </el-form-item>
    <el-form-item label="饲养" prop="rearWorker">
      <el-input
        v-model.trim="form.rearWorker"
        autocomplete="off"
        placeholder="请输入饲养"
      />
    </el-form-item>
    <el-form-item label="孵化" prop="hatchWorker">
      <el-input
        v-model.trim="form.hatchWorker"
        autocomplete="off"
        placeholder="请输入孵化"
      />
    </el-form-item>
    <el-form-item label="产蛋" prop="num">
      <el-input
        v-model.number="form.num"
        autocomplete="off"
        placeholder="请输入产蛋"
        type="number"
      />
    </el-form-item>
    <el-form-item label="破损" prop="wornNum">
      <el-input
        v-model.number="form.wornNum"
        autocomplete="off"
        placeholder="请输入破损"
        type="number"
      />
    </el-form-item>
    <el-form-item label="畸形" prop="malformationNum">
      <el-input
        v-model.number="form.malformationNum"
        autocomplete="off"
        placeholder="请输入畸形"
        type="number"
      />
    </el-form-item>
    <el-form-item label="入孵" prop="broodNum">
      <el-input
        v-model.number="form.broodNum"
        autocomplete="off"
        placeholder="请输入入孵"
        type="number"
      />
    </el-form-item>
    <el-form-item label="商品" prop="productNum">
      <el-input
        v-model.number="form.productNum"
        autocomplete="off"
        placeholder="请输入商品"
        type="number"
      />
    </el-form-item>
    <el-form-item label="照蛋" prop="illuminationTime">
      <el-input
        v-model.trim="form.illuminationTime"
        autocomplete="off"
        placeholder="请输入照蛋"
      />
    </el-form-item>
    <el-form-item label="落盘" prop="placingOrderTime">
      <el-date-picker
        v-model="form.placingOrderTime"
        placeholder="请选择落盘时间"
        style="width: 100%"
        value-format="timestamp"
      />
    </el-form-item>
    <el-form-item label="出雏" prop="broodinNum">
      <el-input
        v-model.number="form.broodinNum"
        autocomplete="off"
        placeholder="请输入出雏"
        type="number"
      />
    </el-form-item>
    <el-form-item label="受精" prop="fertilizedNum">
      <el-input
        v-model.number="form.fertilizedNum"
        autocomplete="off"
        placeholder="请输入受精"
        type="number"
      />
    </el-form-item>
    <el-form-item label="未受" prop="unfertilizedNum">
      <el-input
        v-model.number="form.unfertilizedNum"
        autocomplete="off"
        placeholder="请输入未受"
        type="number"
      />
    </el-form-item>
    <el-form-item label="健雏" prop="healthyNum">
      <el-input
        v-model.number="form.healthyNum"
        autocomplete="off"
        placeholder="请输入健雏"
        type="number"
      />
    </el-form-item>
    <el-form-item v-if="device === 'mobile'" class="submit-content">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from "vuex";
import { API_poultryCreate, API_poultryEdit } from "@/api/poultry";
export default {
  name: "FormEdit",
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        batchCode: "",
        penHouse: "",
        rearWorker: "",
        hatchWorker: "",
        num: "",
        wornNum: "",
        malformationNum: "",
        broodNum: "",
        productNum: "",
        illuminationTime: "",
        placingOrderTime: "",
        broodinNum: "",
        fertilizedNum: "",
        unfertilizedNum: "",
        healthyNum: "",
      },
      rules: {},
    };
  },
  computed: {
    ...mapGetters({
      device: "settings/device",
    }),
  },
  watch: {
    formData: {
      handler(newData) {
        if (newData) {
          this.form = Object.assign({}, this.formData);
        }
      },
      immediate: true,
    },
  },
  methods: {
    close() {
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
      if (this.device === "mobile") {
        this.$router.push({
          name: "Poultry",
          params: { refresh: true },
        });
      }
    },
    save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (this.form.code) {
            const { message } = await API_poultryEdit(this.form);
            this.$baseMessage(message, "success");
          } else {
            const { message } = await API_poultryCreate(this.form);
            this.$baseMessage(message, "success");
          }
          this.$emit("fetch-data");
          this.close();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
